
//
//  ZYUserAccountViewModel.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/11.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

class ZYUserAccountViewModel: NSObject {
    
    static let sharedTools:ZYUserAccountViewModel = ZYUserAccountViewModel()
    
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    // MARK: - 定义一个，防止每次都去 解档 拿数据 ，频繁操作沙盒
    var userAccount:ZYUserAccountModel?
    override init() {
        super.init()
        userAccount = getUserAccount()  //重装app，第一次登陆前，获取为nil
    }
    
    // MARK: access_token - 过期／登录过？判断
    var access_token: String?{
        if userAccount?.access_token != nil {
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                //没过期
                return userAccount?.access_token
            }else{
                //过期了
                return nil
            }
            
        }else{
            //没登录过
            return nil
        }
    }
// MARK: -  属性－登录与否 供visitorView调
    var isLogin:Bool{
        return access_token != nil
    }
    
    
}

//MARK: - 保存数据
extension ZYUserAccountViewModel {
    //保存对象
    func saveUserAccount(userAccount:ZYUserAccountModel){
        //--    //保存userAccount 给缓存
        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
//        print(file)
    }
    
    //获取对象
    func getUserAccount() -> ZYUserAccountModel?{
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? ZYUserAccountModel
        return result
    }
}

// MARK: - 请求数据
extension ZYUserAccountViewModel {
    //获取授权
    func getUserAccount(code:String, finish:(isSuccess:Bool)->()){
        
        ZYNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) -> Void in
            guard let res = response as? [String:AnyObject] else{
                return
            }
            //字典转模型
            let userAccount = ZYUserAccountModel(dic: res)
//            print("getUserAccount成功",userAccount)
            self.getUserInfo(userAccount, finish: finish)
            
            }) { (error) -> Void in
                print("getUserAccount失败")
                finish(isSuccess: false)
        }
        
        
    }
    
    //user/show用户接口---请求用户个人信息
    func getUserInfo(userAccount:ZYUserAccountModel, finish:(isSuccess:Bool)->()){
        
        
        ZYNetworkTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) -> Void in
            guard let res = response as? [String:AnyObject] else{
                return
            }
            // 赋值，保存在model中
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            //归档
            self.saveUserAccount(userAccount)
            
            finish(isSuccess: true)
            
            }) { (error) -> Void in
                print("get usershow fail:\(error)")
                finish(isSuccess: false)
        }
        
    }
    

    
}
